//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SabrSmileSection.h"
using namespace Cephei::QL::Termstructures::Volatility;
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (DateTime d, Double forward, Cephei::IVector<Double>^ sabrParameters, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc) : CSmileSection(CSabrSmileSection::typeid)
{
    CoVector<Double>^ _CsabrParameters;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phSabrSmileSection = NULL;
#endif
        QuantLib::Date _d = (QuantLib::Date)ValueHelper::Convert (d);
        QuantLib::Rate _forward = (QuantLib::Rate)ValueHelper::Convert (forward);
        CoVector<Double>^ _CsabrParameters = safe_cast<CoVector<Double>^> (sabrParameters);
        _CsabrParameters->Lock();
        INativeVector<Double>^ _NCIsabrParameters = _CsabrParameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCsabrParameters = safe_cast<CDoubleVector^>(_NCIsabrParameters);
        std::vector<QuantLib::Real>& _sabrParameters = static_cast<std::vector<QuantLib::Real>&> (_NCsabrParameters->GetReference ());
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dc))
        {
            _Cdc = safe_cast<CDayCounter^> (dc->Value);
            _Cdc->Lock();
        }
        QuantLib::DayCounter& _dc = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>::IsSome::get (dc) ? static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()) : QuantLib::Actual365Fixed()); //1
        _ppSabrSmileSection = new boost::shared_ptr<QuantLib::SabrSmileSection> (new QuantLib::SabrSmileSection ( _d,  _forward,  _sabrParameters,  _dc ));
        SetSmileSection (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CsabrParameters != nullptr) _CsabrParameters->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (Double timeToExpiry, Double forward, Cephei::IVector<Double>^ sabrParameters) : CSmileSection(CSabrSmileSection::typeid)
{
    CoVector<Double>^ _CsabrParameters;
    try
    {
#ifdef HANDLE
        _phSabrSmileSection = NULL;
#endif
        QuantLib::Time _timeToExpiry = (QuantLib::Time)ValueHelper::Convert (timeToExpiry);
        QuantLib::Rate _forward = (QuantLib::Rate)ValueHelper::Convert (forward);
        CoVector<Double>^ _CsabrParameters = safe_cast<CoVector<Double>^> (sabrParameters);
        _CsabrParameters->Lock();
        INativeVector<Double>^ _NCIsabrParameters = _CsabrParameters->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCsabrParameters = safe_cast<CDoubleVector^>(_NCIsabrParameters);
        std::vector<QuantLib::Real>& _sabrParameters = static_cast<std::vector<QuantLib::Real>&> (_NCsabrParameters->GetReference ());
        _ppSabrSmileSection = new boost::shared_ptr<QuantLib::SabrSmileSection> (new QuantLib::SabrSmileSection ( _timeToExpiry,  _forward,  _sabrParameters ));
        SetSmileSection (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CsabrParameters != nullptr) _CsabrParameters->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (boost::shared_ptr<QuantLib::SabrSmileSection>& childNative, Object^ owner) : CSmileSection(CSabrSmileSection::typeid)
{
#ifdef HANDLE
	_phSabrSmileSection = NULL;
#endif
	_ppSabrSmileSection = &childNative;
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (QuantLib::SabrSmileSection& childNative, Object^ owner) : CSmileSection(CSabrSmileSection::typeid)
{
#ifdef HANDLE
	_phSabrSmileSection = NULL;
#endif
	_ppSabrSmileSection = new boost::shared_ptr<QuantLib::SabrSmileSection> (&childNative);
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
    _SabrSmileSectionOwner = owner;
    _SmileSectionOwner = owner;
}

Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (CSabrSmileSection^ copy) : CSmileSection(CSabrSmileSection::typeid)
{
#ifdef HANDLE
	_phSabrSmileSection = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSabrSmileSection = new boost::shared_ptr<QuantLib::SabrSmileSection> (copy->GetShared());
        _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
    }
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (System::Type^ t) : CSmileSection(CSabrSmileSection::typeid)
{
#ifdef HANDLE
	_phSabrSmileSection = NULL;
#endif
	if (!t->IsSubclassOf(CSabrSmileSection::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (QuantLib::Handle<QuantLib::SabrSmileSection>& childNative, Object^ owner)  : CSmileSection(CSabrSmileSection::typeid)
{
	_phSabrSmileSection = &childNative;
	_ppSabrSmileSection = &static_cast<boost::shared_ptr<QuantLib::SabrSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
    _SabrSmileSectionOwner = owner;
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (QuantLib::Handle<QuantLib::SabrSmileSection> childNative)  : CSmileSection(CSabrSmileSection::typeid)
{
	_phSabrSmileSection = &childNative;
	_ppSabrSmileSection = &static_cast<boost::shared_ptr<QuantLib::SabrSmileSection>>(childNative.currentLink());
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::CSabrSmileSection (QuantLib::SabrSmileSection childNative)  : CSmileSection(CSabrSmileSection::typeid)
{
#ifdef HANDLE
	_phSabrSmileSection = NULL;
#endif
	_ppSabrSmileSection = new boost::shared_ptr<QuantLib::SabrSmileSection> (new QuantLib::SabrSmileSection (childNative));
    _ppSmileSection = new boost::shared_ptr<QuantLib::SmileSection> (boost::dynamic_pointer_cast<QuantLib::SmileSection> (*_ppSabrSmileSection));
}
#endif

Cephei::QL::Termstructures::Volatility::CSabrSmileSection::~CSabrSmileSection ()
{
    if (_ppSabrSmileSection != NULL)
    {
	    delete _ppSabrSmileSection;
        _ppSabrSmileSection = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::CSabrSmileSection::!CSabrSmileSection ()
{
    if (_ppSabrSmileSection != NULL)
    {
	    delete _ppSabrSmileSection;
    }
}
QuantLib::SabrSmileSection& Cephei::QL::Termstructures::Volatility::CSabrSmileSection::GetReference ()
{
    if (_ppSabrSmileSection == NULL) throw gcnew NativeNullException ();
	return **_ppSabrSmileSection;
}
boost::shared_ptr<QuantLib::SabrSmileSection>& Cephei::QL::Termstructures::Volatility::CSabrSmileSection::GetShared ()
{
    if (_ppSabrSmileSection == NULL) throw gcnew NativeNullException ();
	return *_ppSabrSmileSection;
}
QuantLib::SabrSmileSection* Cephei::QL::Termstructures::Volatility::CSabrSmileSection::GetPointer ()
{
    if (_ppSabrSmileSection == NULL) throw gcnew NativeNullException ();
	return &**_ppSabrSmileSection;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SabrSmileSection>& Cephei::QL::Termstructures::Volatility::CSabrSmileSection::GetHandle ()
{
	if (_phSabrSmileSection == NULL)
	{
		_phSabrSmileSection = new Handle<QuantLib::SabrSmileSection> (*_ppSabrSmileSection);
	}
	return *_phSabrSmileSection;
}
#endif
bool Cephei::QL::Termstructures::Volatility::CSabrSmileSection::HasNative () 
{
	return (_ppSabrSmileSection != NULL);
}

Double Cephei::QL::Termstructures::Volatility::CSabrSmileSection::AtmLevel::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSabrSmileSection)->atmLevel ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSabrSmileSection::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSabrSmileSection)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::CSabrSmileSection::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSabrSmileSection)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::ISabrSmileSection^ Cephei::QL::Termstructures::Volatility::CSabrSmileSection_Factory::Create (DateTime d, Double forward, Cephei::IVector<Double>^ sabrParameters, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Times::IDayCounter^>^ dc)
{
    return gcnew CSabrSmileSection ( d,  forward,  sabrParameters,  dc);
}
Cephei::QL::Termstructures::Volatility::ISabrSmileSection^ Cephei::QL::Termstructures::Volatility::CSabrSmileSection_Factory::Create (Double timeToExpiry, Double forward, Cephei::IVector<Double>^ sabrParameters)
{
    return gcnew CSabrSmileSection ( timeToExpiry,  forward,  sabrParameters);
}
